package com.tanhua.dubbo.server.api;

import com.alibaba.dubbo.config.annotation.Service;
import com.tanhua.dubbo.server.pojo.RecommendUser;
import com.tanhua.dubbo.server.pojo.Visitors;
import org.bson.types.ObjectId;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Sort;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.Query;

import java.util.List;

/**
 * @Author: JinboYang
 * @Description:
 * @Date: Created in 下午 13:15 2021/1/13
 * @ModifiedBy:
 */
@Service(version = "1.0.0")
public class VisitorsApiImpl implements VisitorsApi {

    @Autowired
    private MongoTemplate mongoTemplate;

    /**
     * @Description: 保存访客
     * @Param:[visitors]
     * @return:java.lang.String
     * @Author:JinboYang
     * @Date:2021/1/13
     */
    @Override
    public String saveVisitor(Visitors visitors) {
        visitors.setId(ObjectId.get());
        visitors.setDate(System.currentTimeMillis());
        this.mongoTemplate.save(visitors);
        return visitors.getId().toHexString();
    }

    @Override
    public List<Visitors> topVisitor(Long userId, Integer num) {
        Query query = Query.query(Criteria.where("userId").is(userId)).with(Sort.by(Sort.Order.desc("date"))).limit(num);
        return this.queryVisitorList(query);
    }

    @Override
    public List<Visitors> topVisitor(Long userId, Long date) {
        Query query = Query.query(Criteria.where("userId").is(userId).and("date").lt(date)).with(Sort.by(Sort.Order.desc("date")));
        return this.queryVisitorList(query);
    }


    /**
     * @Description: 查询得分
     * @Param:[query]
     * @return:java.util.List<com.tanhua.dubbo.server.pojo.Visitors>
     * @Author:JinboYang
     * @Date:2021/1/13
     */
    private List<Visitors> queryVisitorList(Query query) {
        List<Visitors> visitorsList = this.mongoTemplate.find(query, Visitors.class);
        for (Visitors visitors : visitorsList) {
            Query queryRecommendUser = Query.query(Criteria.where("userId").is(visitors.getVisitorUserId()).and("toUserId").is(visitors.getUserId()));
            RecommendUser recommendUser = this.mongoTemplate.findOne(queryRecommendUser, RecommendUser.class);
            if (recommendUser != null) {
                visitors.setScore(recommendUser.getScore());
            } else {
                visitors.setScore(30d);
            }
        }
        return visitorsList;
    }

    /**
     * @Description: 查询访客列表
     * @Param:[userId, page, pageSize]
     * @return:java.util.List<com.tanhua.dubbo.server.pojo.Visitors>
     * @Author:JinboYang
     * @Date:2021/1/16
     */
    @Override
    public List<Visitors> topVisitor(Long userId, Integer page, Integer pageSize) {
        PageRequest pageRequest = PageRequest.of(page - 1, pageSize, Sort.by(Sort.Order.desc("date")));
        Query query = Query.query(Criteria.where("userid").is(userId)).with(pageRequest);
        return this.queryVisitorList(query);
    }
}
